package org.hiucung.baiyun_health_report.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.hiucung.baiyun_health_report.entity.BhrStudent;
import org.hiucung.baiyun_health_report.entity.Result;
import org.hiucung.baiyun_health_report.service.BhrStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  学生账号管理 前端控制器
 * </p>
 *
 * @author kicc
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/bhrstudent")
@CrossOrigin
public class BhrStudentController {

    @Autowired
    private BhrStudentService bhrStudentService;

    @ApiOperation(value = "分页查询学生")
    @GetMapping("pageBhrStudent/{current}/{limit}")
    public Result pageBhrStudent(@PathVariable long current, @PathVariable long limit){
        Page<BhrStudent> pageBhrStudent = new Page<>(current,limit);
        bhrStudentService.page(pageBhrStudent,null);

        long total = pageBhrStudent.getTotal();
        List<BhrStudent> records = pageBhrStudent.getRecords();
        return Result.success().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "查询单个学生")
    @GetMapping("getBhrStudent/{id}")
    public Result getBhrStudent(@PathVariable String id){
        BhrStudent bhrStudent = bhrStudentService.getById(id);
        return Result.success().data("bhrStudent",bhrStudent);
    }

    @ApiOperation(value = "添加学生")
    @PostMapping("addBhrStudent")
    public Result addBhrStudent(@RequestBody BhrStudent bhrStudent){
        boolean save = bhrStudentService.save(bhrStudent);
        if (save){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @ApiOperation(value = "修改学生")
    @PostMapping("updateBhrStudent")
    public Result updateBhrStudent(@RequestBody BhrStudent bhrStudent){
        boolean flag = bhrStudentService.updateById(bhrStudent);
        if (flag){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @ApiOperation(value = "删除学生")
    @DeleteMapping("removeBhrStudent/{id}")
    public Result removeBhrStudent(@PathVariable String id){
        boolean flag = bhrStudentService.removeById(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error();
        }
    }

}

